﻿using Sidvall.Serialization;

namespace Sidvall.AuthorizationManager.Data
{
	public class ClaimItem
	{
		#region Public Members

		#region Value

		private string _Value = string.Empty;
		public string Value
		{
			get
			{
				return _Value;
			}
			set
			{
				value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
				_Value = value;
			}
		}

		#endregion
		#region ComparisonOperator

		private Sidvall.Data.ComparisonOperator _ComparisonOperator = Sidvall.Data.ComparisonOperator.Equal;
		public Sidvall.Data.ComparisonOperator ComparisonOperator
		{
			get
			{
				return _ComparisonOperator;
			}
			set
			{
				_ComparisonOperator = value;
			}
		}

		#endregion

		#region IsMatch

		public bool IsMatch(string value)
		{
			if (value == null)
				value = string.Empty;
			switch (this.ComparisonOperator)
			{
				case Sidvall.Data.ComparisonOperator.Equal:
					if (value.Equals(this.Value, System.StringComparison.OrdinalIgnoreCase))
						return true;
					return false;
				case Sidvall.Data.ComparisonOperator.GreaterThan:
					if (value.ToNullableDouble() > this.Value.ToNullableDouble())
						return true;
					return false;
				case Sidvall.Data.ComparisonOperator.GreaterThanOrEqual:
					if (value.ToNullableDouble() >= this.Value.ToNullableDouble())
						return true;
					return false;
				case Sidvall.Data.ComparisonOperator.LessThan:
					if (value.ToNullableDouble() < this.Value.ToNullableDouble())
						return true;
					return false;
				case Sidvall.Data.ComparisonOperator.LessThanOrEqual:
					if (value.ToNullableDouble() <= this.Value.ToNullableDouble())
						return true;
					return false;
				case Sidvall.Data.ComparisonOperator.NotEqual:
					if (!value.Equals(this.Value, System.StringComparison.OrdinalIgnoreCase))
						return true;
					return false;
				case Sidvall.Data.ComparisonOperator.StartsWith:
					if (value.StartsWith(this.Value, System.StringComparison.OrdinalIgnoreCase))
						return true;
					return false;
				default:
					return false;
			}
		}

		#endregion

		#endregion
		#region Constructors

		public ClaimItem()
		{
		}
		public ClaimItem(string value, Sidvall.Data.ComparisonOperator comparisonOperator)
		{
			this.Value = value;
			this.ComparisonOperator = comparisonOperator;
		}

		#endregion
	}
}
